package com.java46.admin.dessert.dao;

import com.java46.admin.base.exception.BusinessException;
import com.java46.admin.base.util.HibernateUtils;
import com.java46.admin.dessert.entity.DessertAndDessertType;
import com.java46.admin.dessertType.dao.DessertTypeDao;
import com.java46.front.dessert.dao.DessertDao;
import com.java46.front.dessert.entity.DessertEntity;
import com.java46.front.dessertType.entity.DesserttypeEntity;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import java.util.ArrayList;
import java.util.List;

public class AdminDessertDao extends DessertDao {

    /**
     * 分页模糊查询所有商品
     * @return
     */
    public List<DessertAndDessertType> query_Dessert_All(int pageNO,int pageSize,String condition){
        DessertTypeDao dao = new DessertTypeDao();
        //定义集合存放商品和商品类型
        List<DessertAndDessertType> dessertAndDessertTypeList = new ArrayList<>();
        //定义集合存放查询到的商品
        List<DessertEntity> dessertEntityList = new ArrayList<>();
        Session session = HibernateUtils.getSession();
        try{
            Query query = session.createQuery("from com.java46.front.dessert.entity.DessertEntity de where de.dessertName like" + " '%"+condition+"%'");
            query.setFirstResult((pageNO-1)*pageSize);
            query.setMaxResults(pageSize);
            dessertEntityList = query.list();
            for (DessertEntity de:dessertEntityList){
                DessertAndDessertType dessertAndDessertType = new DessertAndDessertType();
                //查询商品类型
                DesserttypeEntity desserttypeEntity  = dao.query_DessertType_ById(de.getDessertType());
                //将商品添加到对象
                dessertAndDessertType.setDessertEntity(de);
                //将类型添加到对象
                dessertAndDessertType.setDesserttypeEntity(desserttypeEntity);
                //将中间实体对象添加到集合
                dessertAndDessertTypeList.add(dessertAndDessertType);
            }
        }catch (Exception e){
            throw new BusinessException("数据库异常...",e);
        }finally {
            session.close();
        }
        return dessertAndDessertTypeList;
    }

    /**
     * 查询商品总条数
     * @return
     */
    public int query_Dessert_getSize(String condition){
        if (condition==null){
            condition="";
        }
        int count =0;
        Session session = HibernateUtils.getSession();
        try{
            String hql = "select count(*)  from com.java46.front.dessert.entity.DessertEntity de where de.dessertName like" + " '%"+condition+"%'";
            Query query = session.createQuery(hql);
            count = ((Long) query.iterate().next()).intValue();
        }catch (Exception e){
            throw new BusinessException("数据库异常...");
        }finally {
            session.close();
        }
        return count;
    }

    /**
     * 删除商品数据
     */
    public void delete_Dessert(int dessertId){
        Session session = HibernateUtils.getSession();
        try{
            Transaction transaction = session.beginTransaction();
            Query query = session.createQuery("delete from com.java46.front.dessert.entity.DessertEntity where dessertId ="+dessertId);
            query.executeUpdate();
            transaction.commit();
        }catch (Exception e){
            throw new BusinessException("数据库连接异常...");
        }finally {
            session.close();
        }
    }
    //根据商品编号查询商品
    public DessertEntity getDessertByDessertId(int dessertId){
        Session session = HibernateUtils.getSession();
        try{
            Query query = session.createQuery("from com.java46.front.dessert.entity.DessertEntity as de where de.dessertId=?1");
            query.setParameter(1,dessertId);
            DessertEntity dessertEntity=(DessertEntity)query.uniqueResult();
            return dessertEntity;
        }catch (Exception e){
            throw new BusinessException("数据库异常...");
        }finally {
            session.close();
        }
    }
    //修改商品
    public void update_dessert(DessertEntity dessertEntity){
        Session session = HibernateUtils.getSession();
        try{
            Transaction transaction = session.beginTransaction();
            Query query = session.createQuery("update com.java46.front.dessert.entity.DessertEntity as d set d.dessertType=?1,d.dessertName=?2,d.dessertPrice=?3,d.desertMark=?4,d.dessertIcon=?5 where d.dessertId =?6");
            query.setParameter(1,dessertEntity.getDessertType());
            query.setParameter(2,dessertEntity.getDessertName());
            query.setParameter(3,dessertEntity.getDessertPrice());
            query.setParameter(4,dessertEntity.getDesertMark());
            query.setParameter(5,dessertEntity.getDessertIcon());
            query.setParameter(6,dessertEntity.getDessertId());
            query.executeUpdate();
            transaction.commit();
        }catch (Exception e){
            throw new BusinessException("数据库连接异常...");
        }finally {
            session.close();
        }
    }

    /**
     * 添加商品
     */
    public void add_Dessert(DessertEntity dessertEntity){
        Session session = HibernateUtils.getSession();
        Transaction transaction = session.beginTransaction();
        session.save(dessertEntity);
        transaction.commit();
        session.close();
    }
}
